package controller;

import dtos.UserDTO;
import dtos.UserlogDTO;
import dtos.UsertypeDTO;
import utils.Codification;
import utils.UserDAORemote;
import utils.UserlogDAORemote;
import utils.UsertypeDAORemote;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Session Bean implementation class Login
 */
@Stateless
@LocalBean
public class Login implements LoginRemote {

    @EJB
    private UserDAORemote userDao;

    @EJB
    private UserlogDAORemote userlogDao;

    @EJB
    private UsertypeDAORemote usertypeDao;

    public Login() {
    }

    /**
     * This method verifies if the user is in the database and if it exists returns his
     * type
     *
     * @param userDTO - a UserDTO containing username and password
     * @return a String containing either the error message or the type of the
     * user
     */

    @Override
    public String verifyUser(UserDTO userDTO, String ending) {
        String mesaj;
        List<UserDTO> listUserDTO = userDao.getAllUsersByUsername(userDTO
                .getUsername());
        if (listUserDTO.isEmpty()) {
            mesaj = "Insucces";
        } else {
            if (!(listUserDTO.get(0).getPassword()
                    .equals(userDTO.getPassword()))) {
                mesaj = "Pasword Mismatch";
            } else {
                if (listUserDTO
                        .get(0)
                        .getPassword()
                        .equals(new Codification().md5(userDTO.getUsername()
                                + ending))) {
                    mesaj = "Pending";
                    System.out.println(" username este "
                            + userDTO.getUsername() + " terminatia este "
                            + ending);

                } else {
                    if (listUserDTO.get(0).getIsActive() == 0) {
                        mesaj = "Account inactiv";
                    } else {
                        mesaj = getUserType(listUserDTO.get(0));
                    }
                }
            }

        }
        System.out.println("login.verifyUser returns " + mesaj);
        return mesaj;
    }

    @Override
    public String getUserType(UserDTO userDto) {
        UsertypeDTO usertypeDto = usertypeDao.getAllUsertypesByUserId(userDto
                .getId());
        return usertypeDto.getName();
    }

    /**
     * This method change user password
     *
     * @param userDTO         - a UserDTO that is currently on session
     * @param retypedPassword - the new password
     * @return userDTO updated with the new password
     */
    @Override
    public UserDTO changePassword(UserDTO userDTO, String retypedPassword) {
        userDTO = setUser(userDTO);
        System.out.println("parola aproape gata " + retypedPassword);
        userDao.updateUserPasswordWithCodification(userDTO, retypedPassword);
        return userDTO;
    }

    /**
     * This method also set the startTime in the database
     *
     * @param userDTO - a UserDTO that contains only Username and password
     * @return a UserDTO instantiated with all needed values
     */
    @Override
    public UserDTO setUser(UserDTO userDTO) {
        UserDTO userDTOFinal;
        List<UserDTO> listUserDTO = userDao.getAllUsersByUsername(userDTO
                .getUsername());
        userDTOFinal = listUserDTO.get(0);
        System.out.println("got user: " + userDTOFinal.getName());
        UserlogDTO userlogDTO = new UserlogDTO();
        userlogDTO.setIdUser(userDTOFinal.getId());
        Date d = new Date();
        DateFormat df1 = new SimpleDateFormat("dd.MM.yyyy");
        DateFormat df2 = new SimpleDateFormat("HH:mm:ss");
        Date data;
        try {
            data = df1.parse(df1.format(d));
            userlogDTO.setDate(data);
            Time t = Time.valueOf(df2.format(d));
            userlogDTO.setStarttime(t);
            userlogDTO.setEndtime(t);

            userlogDao.insertUserlog(userlogDTO);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("got user: " + userDTOFinal.getId() + " "
                + userDTOFinal.getName());

        return userDTOFinal;
    }

    /**
     * This method sets the end time in the database
     *
     * @param userDTO - the UserDTO that represents the user ending session
     */
    @Override
    public void setEndTime(UserDTO userDTO) {
        Date d = new Date();
        DateFormat df2 = new SimpleDateFormat("HH:mm:ss");
        Time t = Time.valueOf(df2.format(d));
        List<UserlogDTO> lista = userlogDao.getAllUserlogsByUserId(userDTO.getId());
        for (UserlogDTO u : lista) {
            if (u.getStarttime().equals(u.getEndtime())) {
                u.setEndtime(t);
                userlogDao.updateUserlog(u);

            }

        }

    }

}